package com.yuelife.service.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuelife.service.dao.SAddressMapper;
import com.yuelife.service.pojo.SAddress;
import com.yuelife.service.service.SAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:admin
 * @Description:SAddress业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class SAddressServiceImpl implements SAddressService {

    @Autowired
    private SAddressMapper sAddressMapper;


    /**
     * SAddress条件+分页查询
     * @param sAddress 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<SAddress> findPage(SAddress sAddress, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(sAddress);
        //执行搜索
        return new PageInfo<SAddress>(sAddressMapper.selectByExample(example));
    }

    /**
     * SAddress分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<SAddress> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<SAddress>(sAddressMapper.selectAll());
    }

    /**
     * SAddress条件查询
     * @param sAddress
     * @return
     */
    @Override
    public List<SAddress> findList(SAddress sAddress){
        //构建查询条件
        Example example = createExample(sAddress);
        //根据构建的条件查询数据
        return sAddressMapper.selectByExample(example);
    }


    /**
     * SAddress构建查询对象
     * @param sAddress
     * @return
     */
    public Example createExample(SAddress sAddress){
        Example example=new Example(SAddress.class);
        Example.Criteria criteria = example.createCriteria();
        if(sAddress!=null){
            // 主键
            if(!StringUtils.isEmpty(sAddress.getAddressid())){
                    criteria.andEqualTo("addressid",sAddress.getAddressid());
            }
            // 用户uid
            if(!StringUtils.isEmpty(sAddress.getUid())){
                    criteria.andEqualTo("uid",sAddress.getUid());
            }
            // 地址详情
            if(!StringUtils.isEmpty(sAddress.getAddressdesc())){
                    criteria.andEqualTo("addressdesc",sAddress.getAddressdesc());
            }
            // 收货姓名
            if(!StringUtils.isEmpty(sAddress.getAddname())){
                    criteria.andLike("addname","%"+sAddress.getAddname()+"%");
            }
            // 收货电话
            if(!StringUtils.isEmpty(sAddress.getAddphone())){
                    criteria.andEqualTo("addphone",sAddress.getAddphone());
            }
            // 存在
            if(!StringUtils.isEmpty(sAddress.getIsdefault())){
                    criteria.andEqualTo("isdefault",sAddress.getIsdefault());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        sAddressMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改SAddress
     * @param sAddress
     */
    @Override
    public void update(SAddress sAddress){
        sAddressMapper.updateByPrimaryKey(sAddress);
    }

    /**
     * 增加SAddress
     * @param sAddress
     */
    @Override
    public void add(SAddress sAddress){
        sAddressMapper.insert(sAddress);
    }

    /**
     * 根据ID查询SAddress
     * @param id
     * @return
     */
    @Override
    public SAddress findById(Integer id){
        return  sAddressMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询SAddress全部数据
     * @return
     */
    @Override
    public List<SAddress> findAll() {
        return sAddressMapper.selectAll();
    }
}
